package cn.zj.logistics.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.zj.logistics.pojo.BaseData;
import cn.zj.logistics.pojo.Customer;
import cn.zj.logistics.pojo.CustomerExample;
import cn.zj.logistics.pojo.MessageObject;
import cn.zj.logistics.pojo.Order;
import cn.zj.logistics.pojo.OrderDetail;
import cn.zj.logistics.pojo.OrderExample;
import cn.zj.logistics.pojo.OrderExample.Criteria;
import cn.zj.logistics.pojo.User;
import cn.zj.logistics.pojo.UserExample;
import cn.zj.logistics.service.BaseDataService;
import cn.zj.logistics.service.CustomerService;
import cn.zj.logistics.service.OrderService;
import cn.zj.logistics.service.UserService;

@Controller
@RequestMapping("/order")
public class OrderController {
	
	
	@Autowired
	private OrderService orderService;
	
	
	@Autowired
	private UserService userService;
	
	
	@Autowired
	private CustomerService customerService;
	
	
	@Autowired
	private BaseDataService baseDataService;

	// 跳转到 order 管理页面
	@RequiresPermissions("order:orderPage")
	@RequestMapping("/orderPage")
	public String orderPage() {
		return "orderPage";
	}
	
	
	
	@ResponseBody
	@RequiresPermissions("order:update")
	@RequestMapping("/update")
	public MessageObject update(Order order) {
		
		
		int row = orderService.updateByPrimaryKeySelective(order);
		
		MessageObject mo = MessageObject.creatMO(0, "修改数据失败，请联系管理员");
		if(row == 1) {
			mo = MessageObject.creatMO(1, "修改数据成功");
		}
		
		return mo;
		
	}
	
	//根据订单id查询出订单明细
	@ResponseBody
	@RequestMapping("/detail")
	public List<OrderDetail> detail(Long orderId){
		
		List<OrderDetail> details = orderService.selectOrderDetailByOrderId(orderId);
		
		
		return details;
	}
	
	/*
	 * @RequestBody SpringMVC底层自动使用jackson
	 *  将前台提交的标准json格式的字符串转换成java对象
	 *  
	 *  前提：必须保证json格式字符串的key和java对象的属性名称相同（规则）
	 *  包括 订单明细的 数组类型
	 * 
	 * 
	 */
	@ResponseBody
	@RequiresPermissions("order:insert")
	@RequestMapping("/insert")
	public MessageObject insert(@RequestBody Order order) {
		
		System.out.println(order);
		int row = orderService.insert(order);
		MessageObject mo = MessageObject.creatMO(0, "新增数据失败，请联系管理员");
		if(row == 1) {
			mo = MessageObject.creatMO(1, "新增数据成功");
		}
		
		return mo;
		
	}
	
	
	/*
	 * jackson+@ResponseBody 方法直接返回json格式的数据
	 * 
	 */
	
	@ResponseBody
	@RequiresPermissions("order:list")
	@RequestMapping("/list")
	public PageInfo<Order> list(@RequestParam(defaultValue = "1")Integer pageNum,
								@RequestParam(defaultValue = "10")Integer pageSize,
								String keyword) {
		PageHelper.startPage(pageNum, pageSize);

		OrderExample example = new OrderExample();

		//开始条件查询
		if(StringUtils.isNotBlank(keyword)) {
			
			Criteria criteria1 = example.createCriteria();
			criteria1.andShippingNameLike("%"+keyword+"%");
			
		}
		
		
		List<Order> orders = orderService.selectByExample(example);

		PageInfo<Order> pageInfo = new PageInfo<>(orders);
		
		return pageInfo;
	}
	
	
	@ResponseBody
	@RequiresPermissions("order:delete")
	@RequestMapping("/delete")
	public MessageObject delete(Long orderId) {
		
		int row = orderService.deleteByPrimaryKey(orderId);
		
		MessageObject mo = MessageObject.creatMO(0, "删除数据失败，请联系管理员！！");
		
		//删除成功
		if(row == 1) {
			mo = MessageObject.creatMO(1, "删除数据成功！！！");
		}
		
		return mo;
	}
	
	
	
	
	/*
	 * 新增或者修改跳转的编辑页面
	 * 
	 */
	
	@RequestMapping("/orderEdit")
	public String orderEdit(Model m,Long orderId) {
		
		
		
		/*
		 * 需要给前台页面共享的数据
		 * 
		 * 1，业务员
		 * 2，客户
		 * 3，区间管理-基础数据表
		 * 4，付款方式-基础数据表
		 * 5，货运方式-基础数据表
		 * 6，取件方式-基础数据表
		 * 7，单位-基础数据表
		 * 
		 */
		//1，业务员
		UserExample example = new UserExample();
		cn.zj.logistics.pojo.UserExample.Criteria criteria = example.createCriteria();
		criteria.andRolenameLike("业务员");
		List<User> users = userService.selectByExample(example);
		m.addAttribute("users", users);
		
		//2，客户
		CustomerExample customrExample = new CustomerExample();
		List<Customer> customers = customerService.selectByExample(customrExample);
		m.addAttribute("customers", customers);
		//3，区间管理-基础数据表
		List<BaseData> intervals = baseDataService.selectBaseDatasByParentName("区间管理");
		m.addAttribute("intervals", intervals);
		// 4，付款方式-基础数据表
		List<BaseData> payments = baseDataService.selectBaseDatasByParentName("付款方式");
		m.addAttribute("payments", payments);
		// 5，货运方式-基础数据表
		List<BaseData> freights = baseDataService.selectBaseDatasByParentName("货运方式");
		m.addAttribute("freights", freights);
		//6，取件方式-基础数据表
		List<BaseData> fetchTypes = baseDataService.selectBaseDatasByParentName("取件方式");
		m.addAttribute("fetchTypes", fetchTypes);
		//7，单位-基础数据表
		List<BaseData> units = baseDataService.selectBaseDatasByParentName("单位");
		m.addAttribute("units", units);
		
		
		/*
		 * 如果是修改，有orderId，要根据id查询出对应的管理员并共享让jsp页面回显
		 */
		if(orderId != null) {
			Order order = orderService.selectByPrimaryKey(orderId);
			m.addAttribute("order", order);
		}
		
		
		
		return "orderEdit";
	}
	
	
	
	
	
	
}
